import 'package:carousel_slider/carousel_slider.dart';
import 'package:efood_multivendor/controller/banner_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/data/model/response/banner_model.dart';
import 'package:efood_multivendor/data/model/response/basic_campaign_model.dart';
import 'package:efood_multivendor/data/model/response/product_model.dart';
import 'package:efood_multivendor/data/model/response/restaurant_model.dart';
import 'package:efood_multivendor/helper/date_converter.dart';
import 'package:efood_multivendor/helper/launch_helper.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/route_helper.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';

import 'package:efood_multivendor/view/base/custom_image.dart';
import 'package:efood_multivendor/view/base/custom_snackbar.dart';
import 'package:efood_multivendor/view/base/product_bottom_sheet.dart';
import 'package:efood_multivendor/view/screens/restaurant/restaurant_screen.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:shimmer_animation/shimmer_animation.dart';

class BannerView extends StatelessWidget {
  final BannerController bannerController;
  BannerView({required this.bannerController});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: GetPlatform.isDesktop ? 500 : 18.h,
      padding: EdgeInsets.only(top: Dimensions.PADDING_SIZE_DEFAULT * 1.1),
      child: bannerController.bannerImageList.isNotEmpty
          ? Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Expanded(
                  child: CarouselSlider.builder(
                    options: CarouselOptions(
                      height: 80.h,
                      aspectRatio: 20 / 9,
                      autoPlay: true,
                      enlargeCenterPage: true,
                      disableCenter: true,
                      autoPlayInterval: Duration(seconds: 7),
                      onPageChanged: (index, reason) {
                        bannerController.setCurrentIndex(index, true);
                      },
                    ),
                    itemCount: bannerController.bannerImageList.length == 0
                        ? 1
                        : bannerController.bannerImageList.length,
                    itemBuilder: (context, index, _) {
                      String? url;
                      if (bannerController.bannerImageList.isEmpty) {
                        url = (bannerController.bannerDataList[index]
                                is BasicCampaignModel
                            ? Get.find<SplashController>()
                                .configModel
                                ?.baseUrls
                                ?.campaignImageUrl
                            : Get.find<SplashController>()
                                .configModel
                                ?.baseUrls
                                ?.bannerImageUrl);
                      } else {
                        url = bannerController.bannerImageList[index];
                      }

                      return InkWell(
                        onTap: () async {
                          var data = bannerController.bannerDataList[index];
                          if (data is WebSiteBanner) {
                            LaunchHelper.launchByBrowser(data.url!);
                          } else if (bannerController.bannerDataList[index]
                              is Product) {
                            Product _product =
                                bannerController.bannerDataList[index];
                            ResponsiveHelper.isMobile(context)
                                ? showModalBottomSheet(
                                    context: context,
                                    isScrollControlled: true,
                                    backgroundColor: Colors.transparent,
                                    builder: (con) =>
                                        ProductBottomSheet(product: _product),
                                  )
                                : showDialog(
                                    context: context,
                                    builder: (con) => Dialog(
                                        child: ProductBottomSheet(
                                            product: _product)),
                                  );
                            await FirebaseAnalytics.instance
                                .logEvent(name: 'banner_clicked', parameters: {
                              'timestamp':
                                  DateConverter.getCurrentTimeInGmtPlus3()
                                      .toString(),
                              'productID': _product.id.toString()
                            });
                          } else if (bannerController.bannerDataList[index]
                              is Restaurant) {
                            Restaurant _restaurant =
                                bannerController.bannerDataList[index];
                            Get.toNamed(
                              RouteHelper.getRestaurantRoute(_restaurant.id!),
                              arguments:
                                  RestaurantScreen(restaurant: _restaurant),
                            );
                            await FirebaseAnalytics.instance
                                .logEvent(name: 'banner_clicked', parameters: {
                              'timestamp':
                                  DateConverter.getCurrentTimeInGmtPlus3()
                                      .toString(),
                              'restaurantID': _restaurant.id.toString()
                            });
                          } else if (bannerController.bannerDataList[index]
                              is BasicCampaignModel) {
                            BasicCampaignModel _campaign =
                                bannerController.bannerDataList[index];
                            Get.toNamed(RouteHelper.getBasicCampaignRoute(
                                _campaign.id!,
                                _campaign.title!,
                                _campaign.image!));
                            await FirebaseAnalytics.instance
                                .logEvent(name: 'banner_clicked', parameters: {
                              'timestamp':
                                  DateConverter.getCurrentTimeInGmtPlus3()
                                      .toString(),
                              'campaignID': _campaign.id.toString()
                            });
                          }
                        },
                        child: Container(
                          width: 95.w,
                          decoration: BoxDecoration(
                            color: Theme.of(context).cardColor,
                            borderRadius: BorderRadius.circular(14),
                            boxShadow: [
                              BoxShadow(
                                  color:
                                      Colors.grey[Get.isDarkMode ? 800 : 200]!,
                                  spreadRadius: 1,
                                  blurRadius: 5)
                            ],
                          ),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(14),
                            child: GetBuilder<SplashController>(
                              builder: (splashController) {
                                return CustomImage(
                                  image: '${url}',
                                  fit: BoxFit.cover,
                                  width: 95.w,
                                );
                              },
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                ),
                SizedBox(height: Dimensions.PADDING_SIZE_EXTRA_SMALL),
                SingleChildScrollView(
                  physics: const NeverScrollableScrollPhysics(),
                  scrollDirection: Axis.horizontal,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: bannerController.bannerImageList.map((bnr) {
                      int index = bannerController.bannerImageList.indexOf(bnr);
                      return TabPageSelectorIndicator(
                        backgroundColor: index == bannerController.currentIndex
                            ? Theme.of(context).colorScheme.secondary
                            : Theme.of(context).disabledColor.withOpacity(0.5),
                        borderColor: Theme.of(context).colorScheme.surface,
                        size: index == bannerController.currentIndex ? 10 : 7,
                      );
                    }).toList(),
                  ),
                ),
              ],
            )
          : Shimmer(
              duration: Duration(seconds: 2),
              enabled: bannerController.bannerImageList.isEmpty,
              child: Container(
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  decoration: BoxDecoration(
                    borderRadius:
                        BorderRadius.circular(Dimensions.RADIUS_SMALL),
                    color: Colors.grey[300],
                  )),
            ),
    );
  }
}
